package com.windy.dailynews.ui.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.windy.dailynews.presenter.NewsDetailContract;
import com.windy.dailynews.ui.activity.ShowWebImageActivity;

/**
 * Created by dingfeng on 2016/12/27.
 */
public class WebViewBase extends WebView {

    private NewsDetailContract.View mView;
    private WebViewClientBase mWebViewClient;
    private WebChromeClientBase mWebChromeClient;

    public WebViewBase(Context context) {
        super(context);
        init(context);
    }

    public WebViewBase(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mWebViewClient = new WebViewClientBase();
        mWebChromeClient = new WebChromeClientBase();
        WebSettings webSettings = getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        // 添加js交互接口类，并起别名 imagelistner
        this.addJavascriptInterface(new MyJavascriptInterface(context), "imagelistner");
        this.setWebViewClient(mWebViewClient);
        this.setWebChromeClient(mWebChromeClient);
    }

    public void setView(NewsDetailContract.View view) {
        mView = view;
    }

    // 遍历所有的img几点，并添加onclick函数，函数的功能是在图片点击的时候调用本地java接口并传递url过去
    String JAVASCRIPT_FUNCTION = "javascript:(function(){" +
            "var objs = document.getElementsByTagName(\"img\"); " +
            "var urls = new Array;" +
            "for(var i=0;i<objs.length;i++)  " +
            "{" +
            "urls[i] = objs[i].src;" +
            "    objs[i].onclick=function()  " +
            "    {  " +
            "        window.imagelistner.openImage(urls.toString(), this.src);  " +
            "    }  " +
            "}" +
            "})()";

    private void addImageClickListener() {
        this.loadUrl(JAVASCRIPT_FUNCTION);
    }

    // js通信接口
    public class MyJavascriptInterface {

        private Context context;

        public MyJavascriptInterface(Context context) {
            this.context = context;
        }

        @JavascriptInterface
        public void openImage(String urls, String currentUrl) {
            Intent intent = new Intent();
            intent.putExtra("images", urls);
            intent.putExtra("current", currentUrl);
            intent.setClass(context, ShowWebImageActivity.class);
            context.startActivity(intent);
        }

    }

    private class WebViewClientBase extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            mView.loadFinish();
            // html加载完成之后，添加监听图片的点击js函数
            addImageClickListener();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }
    }

    private class WebChromeClientBase extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }
    }


}
